package com.nianzhen.weight;


import com.nianzhen.recruitment.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;

/**
 * A subclass of Dialog that can display one, two or three buttons. If you only
 * want to display a String in this dialog box, use the setMessage() method. If
 * you want to display a more complex view, look up the FrameLayout called
 * "custom" and add your view to it:
 * <p/>
 * <pre>
 * FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
 * </pre>
 * <p/>
 * <p/>
 * The AlertDialog class takes care of automatically setting
 * {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
 * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether
 * any views in the dialog return true from {@link View#onCheckIsTextEditor()
 * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog without
 * text editors, so that it will be placed on top of the current input method
 * UI. You can modify this behavior by forcing the flag to your desired mode
 * after calling {@link #onCreate}.
 */
public class MyAlertDialog extends Dialog implements DialogInterface {
    private MyAlertController mAlert;
    private Context mContext;

    protected MyAlertDialog(Context context) {
        this(context, R.style.AlertDialog);
    }

    protected MyAlertDialog(Context context, int theme) {
        super(context, theme);
        mAlert = new MyAlertController(context, this, getWindow());
        mContext = context;
    }

    protected MyAlertDialog(Context context, boolean cancelable,
                            OnCancelListener cancelListener) {
        super(context, R.style.AlertDialog);
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
        mAlert = new MyAlertController(context, this, getWindow());
        mContext = context;
    }

    @Override
    public void show() {

        if (null != mContext && mContext instanceof Activity) {
            Activity at = (Activity) mContext;
            if (at.isFinishing()) {
                return;
            }
        }

        super.show();
    }

    public MyAlertController getMyAlertController() {
        return mAlert;
    }

    /**
     * Gets one of the buttons used in the dialog.
     * <p/>
     * If a button does not exist in the dialog, null will be returned.
     *
     * @param whichButton The identifier of the button that should be returned. For
     *                    example, this can be {@link DialogInterface#BUTTON_POSITIVE}.
     * @return The button from the dialog, or null if a button does not exist.
     */
    public Button getButton(int whichButton) {
        return mAlert.getButton(whichButton);
    }


    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        mAlert.setTitle(title);
    }


    public void setMessage(CharSequence message) {
        mAlert.setMessage(message);
    }

    /**
     * Set the view to display in that dialog.
     */
    public void setView(View view) {
        mAlert.setView(view);
    }

    /**
     * Set the view to display in that dialog, specifying the spacing to appear
     * around that view.
     *
     * @param view              The view to show in the content area of the dialog
     * @param viewSpacingLeft   Extra space to appear to the left of {@code view}
     * @param viewSpacingTop    Extra space to appear above {@code view}
     * @param viewSpacingRight  Extra space to appear to the right of {@code view}
     * @param viewSpacingBottom Extra space to appear below {@code view}
     */
    public void setView(View view, int viewSpacingLeft, int viewSpacingTop,
                        int viewSpacingRight, int viewSpacingBottom) {
        mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight,
                viewSpacingBottom);
    }

    /**
     * Set a message to be sent when a button is pressed.
     *
     * @param whichButton Which button to set the message for, can be one of
     *                    {@link DialogInterface#BUTTON_POSITIVE},
     *                    {@link DialogInterface#BUTTON_NEGATIVE}, or
     *                    {@link DialogInterface#BUTTON_NEUTRAL}
     * @param text        The text to display in positive button.
     * @param msg         The {@link Message} to be sent when clicked.
     */
    public void setButton(int whichButton, CharSequence text, Message msg) {
        mAlert.setButton(whichButton, text, null, msg);
    }

    /**
     * Set a listener to be invoked when the positive button of the dialog is
     * pressed.
     *
     * @param whichButton Which button to set the listener on, can be one of
     *                    {@link DialogInterface#BUTTON_POSITIVE},
     *                    {@link DialogInterface#BUTTON_NEGATIVE}, or
     *                    {@link DialogInterface#BUTTON_NEUTRAL}
     * @param text        The text to display in positive button.
     * @param listener    The {@link DialogInterface.OnClickListener} to use.
     */
    public void setButton(int whichButton, CharSequence text,
                          OnClickListener listener) {
        mAlert.setButton(whichButton, text, listener, null);
    }

    /**
     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
     * {@link DialogInterface#BUTTON_POSITIVE}.
     */
    @Deprecated
    public void setButton(CharSequence text, Message msg) {
        setButton(BUTTON_POSITIVE, text, msg);
    }

    /**
     * Set a listener to be invoked when button 1 of the dialog is pressed.
     *
     * @param text     The text to display in button 1.
     * @param listener The {@link DialogInterface.OnClickListener} to use.
     * @deprecated Use
     * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
     * with {@link DialogInterface#BUTTON_POSITIVE}
     */
    @Deprecated
    public void setButton(CharSequence text, final OnClickListener listener) {
        setButton(BUTTON_POSITIVE, text, listener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAlert.installContent();
    }

    public static class Builder {
        private final MyAlertController.AlertParams P;
        private int mTheme;

        /**
         * Constructor using a context for this builder and the
         * {@link AlertDialog} it creates.
         */
        public Builder(Context context) {
            this(context, R.style.AlertDialog);
        }

        /**
         * Constructor using a context and theme for this builder and the
         * {@link AlertDialog} it creates.
         */
        public Builder(Context context, int theme) {
            P = new MyAlertController.AlertParams(context);
            mTheme = theme;
        }

        /**
         * Set the title using the given resource id.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setTitle(int titleId) {
            P.mTitle = P.mContext.getText(titleId);
            return this;
        }

        /**
         * Set the title displayed in the {@link Dialog}.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setTitle(CharSequence title) {
            P.mTitle = title;
            return this;
        }

        /**
         * Set the message to display using the given resource id.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setMessage(int messageId) {
            P.mMessage = P.mContext.getText(messageId);
            return this;
        }

        /**
         * Set the message to display.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setMessage(CharSequence message) {
            P.mMessage = message;
            return this;
        }

        /**
         * Set the resource id of the {@link Drawable} to be used in the title.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setIcon(int iconId) {
            P.mMessageTopIconId = iconId;
            return this;
        }

        /**
         * Set the {@link Drawable} to be used in the title.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setMessageTopIcon(int resId) {
            P.mMessageTopIconId = resId;
            return this;
        }

        public Builder setMessageLeftIcon(int resId) {
            P.mMessageLeftIconId = resId;
            return this;
        }

        /**
         * Set a listener to be invoked when the positive button of the dialog
         * is pressed.
         *
         * @param textId   The resource id of the text to display in the positive
         *                 button
         * @param listener The {@link DialogInterface.OnClickListener} to use.
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setPositiveButton(int textId,
                                         final OnClickListener listener) {
            P.mPositiveButtonText = P.mContext.getText(textId);
            P.mPositiveButtonListener = listener;
            return this;
        }

        /**
         * Set a listener to be invoked when the positive button of the dialog
         * is pressed.
         *
         * @param text     The text to display in the positive button
         * @param listener The {@link DialogInterface.OnClickListener} to use.
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setPositiveButton(CharSequence text,
                                         final OnClickListener listener) {
            P.mPositiveButtonText = text;
            P.mPositiveButtonListener = listener;
            return this;
        }

        public Builder setPositiveButtonAutoDismiss(boolean bAutoDiss) {
            P.mbAutoDismissPos = bAutoDiss;
            return this;
        }

        /**
         * Sets whether the dialog is cancelable or not default is true.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }

        /**
         * Sets the callback that will be called if the dialog is canceled.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         * @see #setCancelable(boolean)
         */
        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
            P.mOnCancelListener = onCancelListener;
            return this;
        }

        /**
         * Sets the callback that will be called if a key is dispatched to the
         * dialog.
         *
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
            P.mOnKeyListener = onKeyListener;
            return this;
        }

        /**
         * Set a custom view to be the contents of the Dialog. If the supplied
         * view is an instance of a {@link ListView} the light background will
         * be used.
         *
         * @param view The view to use as the contents of the Dialog.
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setView(View view) {
            P.mView = view;
            P.mViewSpacingSpecified = false;
            return this;
        }


        /**
         * Set a custom view to be the contents of the Dialog, specifying the
         * spacing to appear around that view. If the supplied view is an
         * instance of a {@link ListView} the light background will be used.
         *
         * @param view              The view to use as the contents of the Dialog.
         * @param viewSpacingLeft   Spacing between the left edge of the view and the dialog
         *                          frame
         * @param viewSpacingTop    Spacing between the top edge of the view and the dialog
         *                          frame
         * @param viewSpacingRight  Spacing between the right edge of the view and the dialog
         *                          frame
         * @param viewSpacingBottom Spacing between the bottom edge of the view and the dialog
         *                          frame
         * @return This Builder object to allow for chaining of calls to set
         * methods
         * <p/>
         * <p/>
         * This is currently hidden because it seems like people should
         * just be able to put padding around the view.
         * @hide
         */
        public Builder setView(View view, int viewSpacingLeft,
                               int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom) {
            P.mView = view;
            P.mViewSpacingSpecified = true;
            P.mViewSpacingLeft = viewSpacingLeft;
            P.mViewSpacingTop = viewSpacingTop;
            P.mViewSpacingRight = viewSpacingRight;
            P.mViewSpacingBottom = viewSpacingBottom;
            return this;
        }

        /**
         * Sets the Dialog to use the inverse background, regardless of what the
         * contents is.
         *
         * @param useInverseBackground Whether to use the inverse background
         * @return This Builder object to allow for chaining of calls to set
         * methods
         */
        public Builder setInverseBackgroundForced(boolean useInverseBackground) {
            P.mForceInverseBackground = useInverseBackground;
            return this;
        }

        /**
         * @hide
         */
        public Builder setRecycleOnMeasureEnabled(boolean enabled) {
            P.mRecycleOnMeasure = enabled;
            return this;
        }

        /**
         * Creates a {@link AlertDialog} with the arguments supplied to this
         * builder. It does not {@link Dialog#show()} the dialog. This allows
         * the user to do any extra processing before displaying the dialog. Use
         * {@link #show()} if you don't have any other processing to do and want
         * this to be created and displayed.
         */
        public MyAlertDialog create() {
            final MyAlertDialog dialog = new MyAlertDialog(P.mContext, mTheme);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            dialog.setOnCancelListener(P.mOnCancelListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        /**
         * Creates a {@link AlertDialog} with the arguments supplied to this
         * builder and {@link Dialog#show()}'s the dialog.
         */
        public MyAlertDialog show() {
            MyAlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }

    public static void showMessage(Context context, String message) {
        Builder builder = new Builder(context);
        builder.setMessage(message);
        builder.setTitle(R.string.tip);
        builder.setPositiveButton(context.getString(R.string.ok), null);
        builder.setMessageLeftIcon(R.drawable.ic_alert);
        builder.create().show();
    }

    public static void showMessage(Context context, String message, int leftIcon) {
        Builder builder = new Builder(context);
        builder.setMessage(message);
        builder.setTitle(R.string.tip);
        builder.setPositiveButton(context.getString(R.string.ok), null);
        builder.setMessageLeftIcon(leftIcon);
        builder.create().show();
    }
}
